package dice.sinanya.start;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.nlpcn.commons.lang.util.StringUtil;

import java.io.*;
import java.net.URLDecoder;
import java.util.ArrayList;

/**
 * @author SitaNya
 * @date 2020/5/26
 * @email sitanya@qq.com
 * @qqGroup 162279609
 * 有任何问题欢迎咨询
 * <p>
 * 类说明:
 */
public class CoolQConfig implements FindDir {
    private static final Logger logger = LogManager.getLogger(Config.class.getName());

    public CoolQConfig() {
    }

    public boolean checkHttpApiOpen(File fileName) throws IOException {
        if (fileName == null || !fileName.exists()) {
            return false;
        }
        InputStream is = new FileInputStream(fileName);
        BufferedReader in = new BufferedReader(new InputStreamReader(is));
        try {
            String line;
            while ((line = in.readLine()) != null) {
                if ("io.github.richardchien.coolqhttpapi.status=1".equals(line)) {
                    return true;
                }
            }
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        } finally {
            is.close();
            in.close();
        }
        return false;
    }

    @Override
    public File findDir(String fileName) throws IOException {
        File myDir = findMyDir();
        ArrayList<String> pathStr = new ArrayList<>();
        pathStr.add("CoolQ");
        pathStr.add("conf");
        pathStr.add(fileName);
        File conf = new File(URLDecoder.decode(myDir.getPath(), "UTF-8"), StringUtil.joiner(pathStr, File.separator));
        if (conf.exists() && conf.isFile()) {
            return conf;
        }
        return null;
    }
}
